package com.example.servicedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.servicedemo.service.MathService;

/**
 * 绑定服务Demo
 */
public class MainActivity2 extends AppCompatActivity {
    private MathService mathService;
    private final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //绑定成功后通过getService()获取Service实例
            mathService = ((MathService.LocalBinder) iBinder).getService();
        }
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //将Service实例为null，表示绑定意外失效时，Service实例不再可用
            mathService = null;
        }
    };
    private Intent intent;
    //绑定服务
    private void bind() {
        bindService(intent, connection, Context.BIND_AUTO_CREATE);
        isBindService = true;
    }
    //解绑
    private void unbind() {
        unbindService(connection);
        isBindService = false;
    }
    private boolean isBindService = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        intent = new Intent(this, MathService.class);
        Button bindButton = findViewById(R.id.button_bind);
        Button unbindButton = findViewById(R.id.button_unbind);
        Button computeButton = findViewById(R.id.button_compute);
        TextView textView = findViewById(R.id.resultView);


        bindButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isBindService){
                    Toast.makeText(MainActivity2.this, "已经绑定服务", Toast.LENGTH_SHORT).show();
                }else{
                    bind();
                }
            }
        });
        unbindButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isBindService){
                    unbind();
                }else{
                    Toast.makeText(MainActivity2.this, "未绑定服务", Toast.LENGTH_SHORT).show();
                }
            }
        });
        computeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isBindService){
                    long a = Math.round(Math.random() * 100), b = Math.round(Math.random() * 100);
                    long res = mathService.add(a, b);
                    String msg = a + " + " + b + " = " + res;
                    textView.setText(msg);
                }else{
                    Toast.makeText(MainActivity2.this, "未绑定服务", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}